//
//  SAAddShopVC.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/19.
//

#import "SAAddShopVC.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SAAddShopCell.h"
#import "SAProductChooseColorView.h"
#import "SAProductChooseColorModel.h"
#import "SAProductChooseSeasonView.h"
#import "SASelectMerchantsVC.h"
#import "HMSelectPeriodCalendarVC.h"
#import "SAProductChooseImageView.h"
#import "SAProductChooseImageModel.h"
#import "SAProductInventoryView.h"
#import "SAProductSizeORColorModel.h"
#import "SAProductAddAPI.h"
#import "SAProductManufacturerModel.h"
#import "NSString+FintCommon.h"
#import "ScanHelper.h"
#import "SASingeleCodeVC.h"
#import "SASingeleCodeModel.h"
#import "HDDatePickerView.h"
#import "RZRichTextView.h"
#import "SAproductUploadAPI.h"

@interface SAAddShopVC ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIImageView *headerImageV;
@property (nonatomic, strong) UITextField *productCodeTf;
@property (nonatomic, strong) UITextField *productNameTf;
@property (nonatomic, strong) NSMutableArray *titleMarr;
@property (nonatomic, strong) NSMutableArray *rightMarr;
@property (nonatomic, strong) NSMutableArray *typeMarr;
@property (nonatomic, strong) NSMutableArray *colorChooseArray;
@property (nonatomic, strong) NSMutableArray *sizeChooseArray;
@property (nonatomic, strong)NSMutableArray *categoryChooseArray;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) NSMutableArray *imageChooseArray;
@property (nonatomic, strong) NSMutableArray *imageServerChooseArray;

@property (nonatomic, strong) NSMutableArray<SAProductChooseColorList *> *colorSelectArray;//保存被选中的颜色模型
@property (nonatomic, strong) NSMutableArray<SAProductChooseColorList *> *sizeSelectArray;//保存被选中的尺寸模型
@property (nonatomic, strong) NSMutableArray<SAProductSizeORColorModel *> *sizeORColorArray;//保存被选中的库存

@property (nonatomic, strong) SAProductAddAPI *productAddAPI;
@property (nonatomic, assign) NSInteger providerId;
@property (nonatomic, assign) NSInteger isCompositionDetailId;
@property (nonatomic, assign) BOOL isInput;
@property (nonatomic, strong) UIButton *productCodeBtn;

@property (strong, nonatomic) NSMutableArray *colorChooseNameArray;
@property (strong, nonatomic) NSMutableArray *sizeChooseNameArray;
@property (nonatomic, strong) NSMutableArray<SASkuFormModel *> *skuForm;

@property (nonatomic, assign) BOOL isRefresh;


@property (nonatomic, strong) NSMutableArray *seasonChooseArray;
@property (nonatomic, strong) NSMutableArray *brandChooseArray;
@property (nonatomic, strong) NSMutableArray *genderChooseArray;
@property (nonatomic, strong) NSMutableArray *unitChooseArray;

@property (nonatomic, strong) NSMutableArray *contentChooseArray;
@property (nonatomic, strong) NSMutableArray *performChooseArray;
@property (nonatomic, strong) NSMutableArray *securityChooseArray;
@property (nonatomic, strong) NSMutableArray *qualityChooseArray;
@property (nonatomic, strong) NSMutableArray *washingChooseArray;

@property (nonatomic, assign) NSInteger seasonDetailId;
@property (nonatomic, assign) NSInteger brandDetailId;
@property (nonatomic, assign) NSInteger genderDetailId;
@property (nonatomic, assign) NSInteger unitDetailId;

@property (nonatomic, assign) NSInteger contentDetailId;
@property (nonatomic, assign) NSInteger performDetailId;
@property (nonatomic, assign) NSInteger securityDetailId;
@property (nonatomic, assign) NSInteger qualityDetailId;
@property (nonatomic, assign) NSInteger washingDetailId;

@property (nonatomic, strong) RZRichTextView *footview;
@property (nonatomic, strong) SAproductUploadAPI *productUploadAPI;
@property (nonatomic, strong) NSMutableArray *imageMarr;

@end

@implementation SAAddShopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)backToLastVC {
    !self.refreshBlock ?: self.refreshBlock();
    [super backToLastVC];
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [HDHubView dismissLoading];
    [Prompt popPromptViewWithMsg:@"新增成功" duration:2];
    self.titleMarr = nil;
    self.rightMarr = nil;
    self.typeMarr = nil;
    self.productCodeTf.text = @"";
    self.productNameTf.text = @"";
    self.colorChooseArray = nil;
    self.sizeChooseArray = nil;
    self.categoryChooseArray = nil;
    
    self.contentChooseArray = nil;
    self.performChooseArray = nil;
    self.securityChooseArray = nil;
    self.qualityChooseArray = nil;
    self.washingChooseArray = nil;
    
    self.imageChooseArray = nil;
    self.imageServerChooseArray = nil;
    
    self.colorSelectArray = nil;
    self.sizeSelectArray = nil;
    self.sizeORColorArray = nil;
    
    self.colorChooseNameArray = nil;
    self.sizeChooseNameArray = nil;
    self.skuForm = nil;
    
    self.tableView.tableFooterView = nil;
    self.footview.attributedText = nil;
    
    [self.view endEditing:YES];
    
    [self.imageChooseArray removeAllObjects];
    if (self.imageChooseArray.count == 0) {
        self.headerImageV.image = [UIImage imageNamed:@"Shop_Camera"];
        self.headerImageV.contentMode = UIViewContentModeCenter;
        self.headerImageV.clipsToBounds = NO;
    }
    
    self.isRefresh = YES;
    [self.tableView reloadData];
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [HDHubView dismissLoading];
}

#pragma mark - event
//颜色选择
-(void)colorChooseBtnClick {
    [self.view endEditing:YES];
    
    for (SAProductSizeORColorModel *model in self.sizeORColorArray) {
        if ([model.colorId isEqualToString:@"-1"]) {
            [Prompt popPromptViewWithMsg:@"均色已有库存,暂不能选择其他颜色"  duration:2];
            return;
        }
    }
    
    SAProductChooseColorView *chooseColorView = [SAProductChooseColorView shareCornView];
    [chooseColorView loadData:self.colorChooseArray andType:@"1" andIsDetail:NO andArray:self.sizeORColorArray andType:0];
    [chooseColorView showView:self];
    
    __weak typeof(self) weakSelf = self;
    chooseColorView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        NSMutableArray *colorMarr = [NSMutableArray array];
        [weakSelf.colorChooseArray removeAllObjects];
        [weakSelf.colorSelectArray removeAllObjects];
        [weakSelf.colorChooseNameArray removeAllObjects];
        for (SAProductChooseColorModel *productModel in selectedArray) {
            for (SAProductChooseColorList *model in productModel.attrVOS) {
                if (model.check == YES) {
                    [colorMarr addObject:model.attrName];
                    [weakSelf.colorChooseArray addObject:model.specsAttrId];
                    [weakSelf.colorSelectArray addObject:model];
                    [weakSelf.colorChooseNameArray addObject:model.attrName];
                }
            }
        }
        weakSelf.rightMarr[0][0] = [colorMarr componentsJoinedByString:@","];
        //判断是否只有一个sku
        NSArray *sizeArr = [weakSelf.rightMarr[0][1] componentsSeparatedByString:@","];
        if (sizeArr.count < 2 && colorMarr.count < 2) {
            weakSelf.isInput = YES;
        }else {
            weakSelf.isInput = NO;
            if (weakSelf.sizeORColorArray.count > 0) {
                NSMutableArray *strMarr = [NSMutableArray array];
                for (SAProductSizeORColorModel *model in weakSelf.sizeORColorArray) {
                    NSString *str = [NSString stringWithFormat:@"%@ %@ %@件",model.colorName,model.sizeName,model.count];
                    [strMarr addObject:str];
                }
                weakSelf.rightMarr[0][2] = [strMarr componentsJoinedByString:@";"];
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//尺码选择
-(void)sizeChooseBtnClick {
    [self.view endEditing:YES];
    
    for (SAProductSizeORColorModel *model in self.sizeORColorArray) {
        if ([model.sizeId isEqualToString:@"-1"]) {
            [Prompt popPromptViewWithMsg:@"均码已有库存,暂不能选择其他尺码"  duration:2];
            return;
        }
    }
    
    SAProductChooseColorView *chooseColorView = [SAProductChooseColorView shareCornView];
    [chooseColorView loadData:self.sizeChooseArray andType:@"2" andIsDetail:NO andArray:self.sizeORColorArray andType:0];
    [chooseColorView showView:self];
    
    __weak typeof(self) weakSelf = self;
    chooseColorView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        NSMutableArray *sizeMarr = [NSMutableArray array];
        [weakSelf.sizeChooseArray removeAllObjects];
        [weakSelf.sizeSelectArray removeAllObjects];
        [weakSelf.sizeChooseNameArray removeAllObjects];
        for (SAProductChooseColorModel *productModel in selectedArray) {
            for (SAProductChooseColorList *model in productModel.attrVOS) {
                if (model.check == YES) {
                    [sizeMarr addObject:model.attrName];
                    [weakSelf.sizeChooseArray addObject:model.specsAttrId];
                    [weakSelf.sizeSelectArray addObject:model];
                    [weakSelf.sizeChooseNameArray addObject:model.attrName];
                }
            }
        }
        weakSelf.rightMarr[0][1] = [sizeMarr componentsJoinedByString:@","];
        //判断是否只有一个sku
        NSArray *colorArr = [weakSelf.rightMarr[0][0] componentsSeparatedByString:@","];
        if (sizeMarr.count < 2 && colorArr.count < 2) {
            weakSelf.isInput = YES;
        }else {
            weakSelf.isInput = NO;
            if (weakSelf.sizeORColorArray.count > 0) {
                NSMutableArray *strMarr = [NSMutableArray array];
                for (SAProductSizeORColorModel *model in weakSelf.sizeORColorArray) {
                    NSString *str = [NSString stringWithFormat:@"%@ %@ %@件",model.colorName,model.sizeName,model.count];
                    [strMarr addObject:str];
                }
                weakSelf.rightMarr[0][2] = [strMarr componentsJoinedByString:@";"];
            }
        }
        [weakSelf.tableView reloadData];
    };
}



//选择图片
-(void)ChooseImage {
    [self.view endEditing:YES];
    SAProductChooseImageView *chooseImageView = [SAProductChooseImageView shareCornView];
    [chooseImageView loadData:self.imageChooseArray andServeArray:self.imageServerChooseArray andType:@"选择图片"];
    [chooseImageView showView:self];
    __weak typeof(self) weakSelf = self;
    chooseImageView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray, NSMutableArray * _Nonnull serverArray) {
        weakSelf.imageChooseArray = selectedArray;
        weakSelf.imageServerChooseArray = serverArray;
        for (SAProductChooseImageModel *model in selectedArray) {
            if (model.isSelecd == YES) {
                [weakSelf.headerImageV sd_setImageWithURL:[NSURL URLWithString:model.url]];
                weakSelf.headerImageV.contentMode = UIViewContentModeScaleAspectFit;
            }
        }
        if (weakSelf.imageChooseArray.count == 0) {
            weakSelf.headerImageV.image = [UIImage imageNamed:@"Shop_Camera"];
            weakSelf.headerImageV.contentMode = UIViewContentModeCenter;
        }
    };
}

//库存选择
-(void)inventoryChooseBtnClick {
    [self.view endEditing:YES];
    SAProductInventoryView *productInventoryView = [[SAProductInventoryView alloc] initWithColourArr:self.colorSelectArray SizeArr:self.sizeSelectArray AndSelectArr:self.sizeORColorArray];
    [productInventoryView showView:self];
    __weak typeof(self) weakSelf = self;
    productInventoryView.selectBlock = ^(NSMutableArray<SAProductSizeORColorModel *> * _Nonnull marr) {
        weakSelf.sizeORColorArray = marr;
        if (marr.count > 0) {
            NSMutableArray *strMarr = [NSMutableArray array];
            for (SAProductSizeORColorModel *model in marr) {
                NSString *str = [NSString stringWithFormat:@"%@ %@ %@件",model.colorName,model.sizeName,model.count];
                [strMarr addObject:str];
            }
            weakSelf.rightMarr[0][2] = [strMarr componentsJoinedByString:@";"];
            [weakSelf.tableView reloadData];
        }
    };
}

//季节选择
-(void)seasonChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.seasonChooseArray andType:@"1" andIsDetail:@"1" andIsDetailId:self.seasonDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.seasonChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][4] = list.attrName;
                weakSelf.seasonDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//品牌选择
-(void)brandChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.brandChooseArray andType:@"2" andIsDetail:@"1" andIsDetailId:self.brandDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.brandChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][5] = list.attrName;
                weakSelf.brandDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//性别选择
-(void)genderChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.genderChooseArray andType:@"3" andIsDetail:@"1" andIsDetailId:self.genderDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.genderChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][6] = list.attrName;
                weakSelf.genderDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//单位选择
-(void)unitChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.unitChooseArray andType:@"-1" andIsDetail:@"1" andIsDetailId:self.unitDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.unitChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][7] = list.unitName;
                weakSelf.unitDetailId = list.unitId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//类别选择
-(void)typeChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.categoryChooseArray andType:@"4" andIsDetail:@"1" andIsDetailId:self.isCompositionDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.categoryChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][8] = list.attrName;
                weakSelf.isCompositionDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//选择厂家
-(void)SelectMerchantsBtnClick {
    __weak typeof(self) weakSelf = self;
    [self.view endEditing:YES];
    
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *isSupplier = [NSString stringWithFormat:@"%@",menuList[7][@"isDelete"]];
    NSString *seeProvider = [NSString stringWithFormat:@"%@",menuList[7][@"perms"][@"seeProvider"]];
    if ([isSupplier isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有供货商权限,请联系店长添加" duration:2];
        return;
    }
    if ([seeProvider isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有供货商权限,请联系店长添加" duration:2];
        return;
    }
    
    SASelectMerchantsVC *pushVC = [SASelectMerchantsVC new];
    pushVC.selectBlock = ^(SAProductManufacturerModel *model) {
        weakSelf.providerId = model.providerId;
        weakSelf.rightMarr[3][9] = model.providerName;
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

//成分含量
-(void)compositionChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.contentChooseArray andType:@"5" andIsDetail:@"1" andIsDetailId:self.contentDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.contentChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][10] = list.attrName;
                weakSelf.contentDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//执行标准
-(void)performChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.performChooseArray andType:@"6" andIsDetail:@"1" andIsDetailId:self.performDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.performChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][11] = list.attrName;
                weakSelf.performDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//安全类别
-(void)securityChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.securityChooseArray andType:@"7" andIsDetail:@"1" andIsDetailId:self.securityDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.securityChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][12] = list.attrName;
                weakSelf.securityDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//质量等级
-(void)qualityChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.qualityChooseArray andType:@"8" andIsDetail:@"1" andIsDetailId:self.qualityDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.qualityChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][13] = list.attrName;
                weakSelf.qualityDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//洗涤方式
-(void)washingChooseBtnClick {
    [self.view endEditing:YES];
    SAProductChooseSeasonView *compositionChooseView = [SAProductChooseSeasonView shareCornView];
    [compositionChooseView loadData:self.washingChooseArray andType:@"9" andIsDetail:@"1" andIsDetailId:self.washingDetailId];
    UIViewController *VC = nil;
    [compositionChooseView showView:VC];
    __weak typeof(self) weakSelf = self;
    compositionChooseView.selectedBlock = ^(NSMutableArray * _Nonnull selectedArray) {
        weakSelf.washingChooseArray = selectedArray;
        for (SAProductChooseColorList *list in selectedArray) {
            if (list.check == YES) {
                weakSelf.rightMarr[3][14] = list.attrName;
                weakSelf.washingDetailId = list.attrId;
            }
        }
        [weakSelf.tableView reloadData];
    };
}

//选择日期
-(void)selectPeriodCalendar {
    __weak typeof(self) weakSelf = self;
    [self.view endEditing:YES];
    
    HDDatePickerView *pickerView = [[HDDatePickerView alloc] initWithSlectedDate:weakSelf.rightMarr[3][15]];
    [pickerView show];
    pickerView.selectDateBlock = ^(NSString *selectedDate) {
        weakSelf.rightMarr[3][15] = selectedDate;
        [weakSelf.tableView reloadData];
    };
    
//    HMSelectPeriodCalendarVC *pushVC = [HMSelectPeriodCalendarVC new];
//    pushVC.lastDate = YES;
//    pushVC.selectBlock = ^(NSString *selectDate) {
//        weakSelf.rightMarr[3][4] = selectDate;
//        [weakSelf.tableView reloadData];
//    };
//    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedProductCodeBtn {
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        self.productCodeTf.text = result;
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

//扫码
-(void)scanProductCode {
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        self.rightMarr[2][0] = result;
        [self.tableView reloadData];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

//单品条码
-(void)singleCode:(NSMutableArray *)marr {
    NSString *str = @"";
    NSMutableArray<SASkuFormModel *> *skuFormMarr = [NSMutableArray array];
    for (SASingeleCodeModel *model in marr) {
        for (SASkuFormModel *skuModel in model.skuMarr) {
            if (skuModel.barcode.length > 0) {
                str = @"已设置";
            }
            [skuFormMarr addObject:skuModel];
        }
    }
    self.rightMarr[2][1] = str;
    self.skuForm = skuFormMarr;
    [self.tableView reloadData];
}

-(void)clickedConfirmBtn {
    
    [self.view endEditing:YES];
    if (trimString(self.productNameTf.text).length <= 0) {
        [Prompt popPromptViewWithMsg:@"请输入名称" duration:2];
        return;
    }
    
    NSArray *imageArr = self.footview.rz_richTextImages;
    self.imageMarr = [NSMutableArray array];
    
    if (imageArr.count > 0) {
        [HDHubView loadingMessage:@"上传中..."];
        [self.productUploadAPI postDataWithUrl:@"" params:nil imageDatas:imageArr success:^(id response) {
            NSLog(@"%@",response);
            if ([response[@"code"] integerValue] == 0) {
                [HDHubView dismissLoading];
                self.imageMarr = response[@"data"];
                [self clickedConfirmBtn2];
            }
        } failure:^(id error) {
            [HDHubView dismissLoading];
        }];
    }else {
        [self clickedConfirmBtn2];
    }
}

-(void)clickedConfirmBtn2 {
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[@"productName"] = trimString(self.productNameTf.text);
    if (trimString(self.productCodeTf.text).length > 0) {
        dict[@"productCode"] = trimString(self.productCodeTf.text);
    }else {
        dict[@"productCode"] = [self generateProductCode];
    }
    
    if (self.unitChooseArray.count >0) {
        for ( SAProductChooseColorList *list in self.unitChooseArray) {
            if (list.check == YES) {
                dict[@"unitId"] =@(list.unitId);
                dict[@"unitName"] =list.unitName;
            }
        }
    }
    
    NSString *barcode = self.rightMarr[2][0];
    if (barcode.length == 0) {
        barcode = [NSString stringWithFormat:@"%@%@",[self generateTradeNO],dict[@"productCode"]];
    }
    dict[@"barcode"] = barcode;
    
    dict[@"upper"] = @(true);
    
    if ([self.rightMarr[1][0] length] >0) {
        dict[@"entryPrice"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][0] doubleValue]*100];
    }
    if ([self.rightMarr[1][1] length] >0) {
        dict[@"retailPrice"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][1] doubleValue]*100];
    }
    if ([self.rightMarr[1][2] length] >0) {
        dict[@"microStorePrice"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][2] doubleValue]*100];
    }
    
    if ([self.rightMarr[1][3] length] >0) {
        dict[@"discountRate"] = [NSString stringWithFormat:@"%0.lf",[self.rightMarr[1][3] doubleValue]];
    }
    
    if (self.imageServerChooseArray.count >0) {
        dict[@"imgUrl"] = self.imageServerChooseArray;
        for (int i=0; i<self.imageChooseArray.count; i++) {
            SAProductChooseImageModel *model = self.imageChooseArray[i];
            if (model.isSelecd == YES) {
                dict[@"imgIndex"] = @(i);
            }
        }
    }
    dict[@"onsalesTime"] = self.rightMarr[3][15];
    dict[@"isMicroStoreProduct"] = self.rightMarr[3][16];
    dict[@"ground"] = self.rightMarr[3][0];
    dict[@"dealer"] = self.rightMarr[3][1];
//    dict[@"microStoreIntro"] = [self.footview rz_codingToHtmlWithImageURLS:self.imageMarr];
    
    if (trimString(self.rightMarr[3][2]).length >0) {
        dict[@"stockUp"] = @([self.rightMarr[3][2] integerValue]);
    }
    if (trimString(self.rightMarr[3][3]).length >0) {
        dict[@"stockDown"] = @([self.rightMarr[3][3] integerValue]);
    }
    
    NSMutableArray *chooseArray = [[NSMutableArray alloc] init];
    if (self.seasonChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.seasonChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(1);
                chooseDict[@"attrName"] =list.attrName;
                chooseDict[@"check"] = @"true";
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.brandChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.brandChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(2);
                chooseDict[@"attrName"] =list.attrName;
                chooseDict[@"check"] = @"true";
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.genderChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.genderChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(3);
                chooseDict[@"attrName"] =list.attrName;
                chooseDict[@"check"] = @"true";
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.categoryChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.categoryChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(4);
                chooseDict[@"attrName"] =list.attrName;
                chooseDict[@"check"] = @"true";
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.contentChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.contentChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(5);
                chooseDict[@"attrName"] =list.attrName;
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.performChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.performChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(6);
                chooseDict[@"attrName"] =list.attrName;
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.securityChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.securityChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(7);
                chooseDict[@"attrName"] =list.attrName;
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.qualityChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.qualityChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(8);
                chooseDict[@"attrName"] =list.attrName;
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (self.washingChooseArray.count >0) {
        NSMutableDictionary *chooseDict = [[NSMutableDictionary alloc] init];
        for ( SAProductChooseColorList *list in self.washingChooseArray) {
            if (list.check == YES) {
                chooseDict[@"attrId"] =@(list.attrId);
                chooseDict[@"attrType"] =@(9);
                chooseDict[@"attrName"] =list.attrName;
                [chooseArray addObject:chooseDict];
            }
        }
    }
    
    if (chooseArray.count > 0) {
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:chooseArray options:NSJSONWritingPrettyPrinted error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        dict[@"toAttrForms"] = jsonString;
    }

    if (trimString(self.rightMarr[3][9]).length >0) {
        dict[@"providerId"] = @(self.providerId);
    }
    
    //添加库存
    NSMutableArray *colorAndSizeArray = [NSMutableArray array];
    
    NSMutableArray<SAProductChooseColorList *> *selectColourMarr = [NSMutableArray array];
    if (self.colorSelectArray.count > 0) {
        selectColourMarr = self.colorSelectArray;
    }else {
        SAProductChooseColorList *model = [SAProductChooseColorList new];
        model.specsAttrId = @"-1";
        model.attrName = @"均色";
        [selectColourMarr addObject:model];
    }
    
    NSMutableArray<SAProductChooseColorList *> *selectSizeMarr = [NSMutableArray array];
    if (self.sizeSelectArray.count > 0) {
        selectSizeMarr = self.sizeSelectArray;
    }else {
        SAProductChooseColorList *model = [SAProductChooseColorList new];
        model.specsAttrId = @"-1";
        model.attrName = @"均码";
        [selectSizeMarr addObject:model];
    }
    
    long max = 1000;
    for (SASkuFormModel *skuFormModel in self.skuForm) {
        NSString *barcode = skuFormModel.barcode;
        if ([barcode containsString:dict[@"productCode"]]) {
            barcode = [barcode stringByReplacingOccurrencesOfString:dict[@"productCode"] withString:@""];
            long barcodeD = [barcode longLongValue];
            if (barcodeD > max) {
                max = barcodeD;
            }
        }
    }
    
    NSInteger i = 0;
    NSInteger j = 0;
    for (SAProductChooseColorList *colorModel in selectColourMarr) {
        for (SAProductChooseColorList *sizeModel in selectSizeMarr) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            NSString *colorSpecsAttrId = colorModel.specsAttrId;
            NSString *sizeSpecsAttrId = sizeModel.specsAttrId;
            if ([colorSpecsAttrId isEqualToString:@"-1"]) {
                colorSpecsAttrId = @"1"; //兼容改动
            }
            if ([sizeSpecsAttrId isEqualToString:@"-1"]) {
                sizeSpecsAttrId = @"2";//兼容改动
            }
            dic[@"colourId"]  = colorSpecsAttrId;
            dic[@"sizeId"]  = sizeSpecsAttrId;
            dic[@"stock"]  = @"0";
            for (SAProductSizeORColorModel *sizeORColorMode in self.sizeORColorArray) {
                if ([sizeModel.specsAttrId isEqualToString:sizeORColorMode.sizeId] && [colorModel.specsAttrId isEqualToString:sizeORColorMode.colorId]) {
                    dic[@"stock"] = sizeORColorMode.count;
                }
            }
            NSString *barcode = @"";
            for (SASkuFormModel *skuFormModel in self.skuForm) {
                if ([colorSpecsAttrId isEqualToString:skuFormModel.colourId] && [sizeSpecsAttrId isEqualToString:skuFormModel.sizeId]) {
                    barcode = skuFormModel.barcode;
                }
            }
            if (barcode.length > 0) {
                dic[@"barcode"] = barcode;
            }else {
                max =  max + 1;
                dic[@"barcode"] = [NSString stringWithFormat:@"%ld%@",max,dict[@"productCode"]];
            }
            j++;

            [colorAndSizeArray addObject:dic];
        }
        i++;
        j = 0;
    }
    dict[@"skuForm"] = colorAndSizeArray;
    
    self.productAddAPI.coverParameters = dict;
    [HDHubView loadingMessage:@"正在保存"];
    [self.productAddAPI sendRequestToServerWithActivityViewAndMask:POST];
}

- (NSString *)generateProductCode
{
    static int kNumber = 7;
    NSString *sourceStr = @"0123456789";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    NSString *index = [self.productNameTf.text charactorGetFirstCharactor:YES];
    if (![self validateNumber:index]) {
        index = @"A";
    }
    NSString *newStr = [NSString stringWithFormat:@"%@%@",index,resultStr];
    return newStr;
}

- (NSString *)generateTradeNO
{
    static int kNumber = 3;
    NSString *sourceStr = @"0123456789";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678\\9*-"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *marr = self.titleMarr[section];
    return marr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAAddShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAAddShopCell" forIndexPath:indexPath];
    NSString *leftStr = self.titleMarr[indexPath.section][indexPath.row];
    NSString *rightStr = self.rightMarr[indexPath.section][indexPath.row];
    NSString *type = self.typeMarr[indexPath.section][indexPath.row];
    [cell refreshLeftTitle:leftStr RightTitle:rightStr Type:type.integerValue AndIsInput:self.isInput];
    
    __weak typeof(self) weakSelf = self;
    cell.inputBlock = ^(NSString * _Nonnull str) {
        if (indexPath.section == 0 && indexPath.row == 2) {
            SAProductSizeORColorModel *model = [SAProductSizeORColorModel new];
            if (weakSelf.sizeSelectArray.count == 0) {
                model.sizeId = @"-1";
                model.sizeName = @"均码";
            }else {
                model.sizeId = weakSelf.sizeSelectArray[0].specsAttrId;
                model.sizeName = weakSelf.sizeSelectArray[0].attrName;
            }
            if (weakSelf.colorSelectArray.count == 0) {
                model.colorId = @"-1";
                model.colorName = @"均色";
            }else {
                model.colorId = weakSelf.colorSelectArray[0].specsAttrId;
                model.colorName = weakSelf.colorSelectArray[0].attrName;
            }
            model.count = str;
            [weakSelf.sizeORColorArray removeAllObjects];
            if (str.length > 0) {
                [weakSelf.sizeORColorArray addObject:model];
            }
            weakSelf.rightMarr[indexPath.section][indexPath.row] = str;
        }else {
            weakSelf.rightMarr[indexPath.section][indexPath.row] = str;
        }
    };
    cell.productCodeBlock = ^{
        [weakSelf scanProductCode];
    };
    
    cell.singleCodeBlock = ^{
        NSMutableArray<SASingeleCodeModel *> *singleMarr = [NSMutableArray array];
        NSMutableArray *colorChooseMarr = [NSMutableArray array];
        NSMutableArray *colorChooseNameMarr = [NSMutableArray array];
        for (NSString *str in weakSelf.colorChooseArray) {
            [colorChooseMarr addObject:str];
        }
        for (NSString *str in weakSelf.colorChooseNameArray) {
            [colorChooseNameMarr addObject:str];
        }
        if (colorChooseMarr.count == 0) {
            [colorChooseMarr addObject:@"1"];
            [colorChooseNameMarr addObject:@"均色"];
        }
        
        NSMutableArray *sizeChooseMarr = [NSMutableArray array];
        NSMutableArray *sizeChooseNameMarr = [NSMutableArray array];
        for (NSString *str in weakSelf.sizeChooseArray) {
            [sizeChooseMarr addObject:str];
        }
        for (NSString *str in weakSelf.sizeChooseNameArray) {
            [sizeChooseNameMarr addObject:str];
        }
        if (sizeChooseMarr.count == 0) {
            [sizeChooseMarr addObject:@"2"];
            [sizeChooseNameMarr addObject:@"均码"];
        }
        
        for (int i = 0; i < colorChooseMarr.count; i++) {
            SASingeleCodeModel *model = [SASingeleCodeModel new];
            model.colourSpecsAttrId = colorChooseMarr[i];
            model.colourAttrName = colorChooseNameMarr[i];
            model.skuMarr = [NSMutableArray array];
            [singleMarr addObject:model];
            for (int j = 0; j < sizeChooseMarr.count; j++) {
                SASkuFormModel *skuModel = [SASkuFormModel new];
                skuModel.colourId = colorChooseMarr[i];
                skuModel.colourName = colorChooseNameMarr[i];
                skuModel.sizeId = sizeChooseMarr[j];
                skuModel.sizeName = sizeChooseNameMarr[j];
                skuModel.barcode = @"";
                [model.skuMarr addObject:skuModel];
                for (SASkuFormModel *skuFormModel in weakSelf.skuForm) {
                    if ([skuModel.colourId isEqualToString:skuFormModel.colourId] && [skuModel.sizeId isEqualToString:skuFormModel.sizeId]) {
                        skuModel.barcode = skuFormModel.barcode;
                    }
                }
            }
        }
        SASingeleCodeVC *pushVC = [[SASingeleCodeVC alloc] initWithSingeleCodeModelMarr:singleMarr];
        [weakSelf.navigationController pushViewController:pushVC animated:YES];
        pushVC.confirmBlock = ^(NSMutableArray<SASingeleCodeModel *> * _Nonnull marr) {
            [weakSelf singleCode:marr];
        };
    };
    
    cell.onBlock = ^(NSString * _Nonnull index) {
            weakSelf.rightMarr[3][indexPath.row] = index;
            if ([index isEqualToString:@"1"]) {
//                tableView.tableFooterView = weakSelf.footview;
                tableView.tableFooterView = nil;
            }else {
                tableView.tableFooterView = nil;
            }
        [tableView reloadData];
    };
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0){
        [self colorChooseBtnClick];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        [self sizeChooseBtnClick];
    }else if (indexPath.section == 0 && indexPath.row == 2){
        if (self.isInput != YES) {
            [self inventoryChooseBtnClick];
        }
    }else if (indexPath.section == 3 && indexPath.row == 4){
        [self seasonChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 5){
        [self brandChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 6){
        [self genderChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 7){
        [self unitChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 8){
        [self typeChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 9){
        [self SelectMerchantsBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 10){
        [self compositionChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 11){
        [self performChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 12){
        [self securityChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 13){
        [self qualityChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 14){
        [self washingChooseBtnClick];
    }else if (indexPath.section == 3 && indexPath.row == 15){
        [self selectPeriodCalendar];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == self.titleMarr.count -1) {
        NSMutableArray *marr = self.rightMarr[section];
        NSString *rightTitle = marr.lastObject;
        if ([rightTitle isEqualToString:@"1"]) {
            return 0.001;
        }
        return SafeFooterHeight;
    }
    return kScaleNum(10);
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 14) {
        return NO;
    }
    
    return [self validateNumber:string];
}

- (void)textFieldEditChanged:(UITextField*)textField {
    NSString *newStr = [textField.text noEmoji];
    if (textField.text.length != newStr.length) {
        textField.text = newStr;
    }
}

#pragma mark - initUI
-(void)initUI {
    self.isInput = YES;
    
    [self setNavigationItemBackBBIAndTitle:@"新增商品"];
    [self addRightViews:@[self.confirmBtn]];
    
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView addSubview:self.headerImageV];
    [self.headerView addSubview:self.productCodeTf];
    [self.headerView addSubview:self.productNameTf];
//    [self.headerView addSubview:self.productCodeBtn];
    
    RZRichTextView *footview = [[RZRichTextView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 245)];
    footview.font = [UIFont systemFontOfSize:16.0];
    self.footview = footview;
    self.tableView.tableFooterView = nil;
    self.footview.bounces = NO;
    self.tableView.bounces = NO;
    footview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    UILabel *placeholderLb = [[UILabel alloc] init];
    placeholderLb.font = self.footview.font;
    placeholderLb.textColor = kGrayColor;
    placeholderLb.text = @"请输入文字或插入图片";
    placeholderLb.numberOfLines = 0;
    [footview addSubview:placeholderLb];
    [footview setValue:placeholderLb forKey:@"_placeholderLabel"];
    footview.text = @"1";
    footview.text = @"";
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kColor(246, 246, 246, 1);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAAddShopCell class] forCellReuseIdentifier:@"SAAddShopCell"];
    }
    return _tableView;
}

-(UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 105)];
        _headerView.backgroundColor = kColor(246, 246, 246, 1);
        UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, 8, kBaseScreenWidth, 80)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:whiteView];
        UILabel *numberLab = [[UILabel alloc] initWithFrame:SARectMake(99, 48, 50, 40)];
        numberLab.textAlignment = NSTextAlignmentLeft;
        numberLab.text = @"款号";
        numberLab.font = kFont(14);
        numberLab.textColor = [UIColor blackColor];
        [_headerView addSubview:numberLab];
        UILabel *nameLab = [[UILabel alloc] initWithFrame:SARectMake(99, 8, 50, 40)];
        nameLab.textAlignment = NSTextAlignmentLeft;
        nameLab.text = @"名称*";
        nameLab.font = kFont(14);
        nameLab.textColor = [UIColor blackColor];
        [_headerView addSubview:nameLab];
        [nameLab setAttributedStringWithSubString:@"*" color:kRedColor];
        UIView *line = [[UIView alloc] initWithFrame:SARectMake(99, 47, 276, 1)];
        line.backgroundColor = kLineColor;
        [_headerView addSubview:line];
    }
    return _headerView;
}

-(UIImageView *)headerImageV {
    if (!_headerImageV) {
        _headerImageV = [[UIImageView alloc] initWithFrame:SARectMake(25, 12, 64, 72)];
        _headerImageV.image = [UIImage imageNamed:@"Shop_Camera"];
        _headerImageV.contentMode = UIViewContentModeCenter;
        _headerImageV.clipsToBounds = YES;
        _headerImageV.backgroundColor = kColor(246, 246, 246, 1);
        _headerImageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ChooseImage)];
        [_headerImageV addGestureRecognizer:tap];
    }
    return _headerImageV;
}

-(UITextField *)productCodeTf {
    if (!_productCodeTf) {
        _productCodeTf = [[UITextField alloc] initWithFrame:SARectMake(150, 48, 200, 40)];
        _productCodeTf.font = kFont(14);
        _productCodeTf.textAlignment = NSTextAlignmentRight;
        _productCodeTf.textColor = kGrayColor;
        _productCodeTf.placeholder = @"不输入款号时，系统自动生成";
        _productCodeTf.delegate = self;
        _productCodeTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _productCodeTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_productCodeTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _productCodeTf;
}

-(UIButton *)productCodeBtn {
    if (!_productCodeBtn) {
        _productCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _productCodeBtn.frame = SARectMake(320, 48, 36, 40);
        [_productCodeBtn setImage:[UIImage imageNamed:@"gray_san"] forState:UIControlStateNormal];
        [_productCodeBtn addTarget:self action:@selector(clickedProductCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _productCodeBtn;
}

-(UITextField *)productNameTf {
    if (!_productNameTf) {
        _productNameTf = [[UITextField alloc] initWithFrame:SARectMake(150, 8, 200, 40)];
        _productNameTf.font = kFont(14);
        _productNameTf.textAlignment = NSTextAlignmentRight;
        _productNameTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        _productNameTf.textColor = kGrayColor;
        _productNameTf.placeholder = @"必填";
        [_productNameTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _productNameTf;
}

-(NSMutableArray *)titleMarr {
    if (!_titleMarr) {
        _titleMarr = [NSMutableArray array];
        NSArray *arr1 = @[@"颜色", @"尺码", @"初始库存"];
        NSArray *arr2 = @[@"入库价", @"实体零售价", @"微店价", @"折扣"];
        NSArray *arr3 = @[@"商品条码", @"单品条码"];
//        NSArray *arr4 = @[@"类别", @"供货商", @"库存上限", @"库存下限", @"上架日期"];
        NSArray *arr4 = @[@"场地", @"经销商", @"库存上限", @"库存下限",@"季节", @"品牌", @"性别", @"单位", @"类别", @"供货商", @"成分含量", @"执行标准", @"安全类别", @"质量等级",  @"洗涤方式", @"上架日期", @"发布微商城"];
        [_titleMarr addObject:arr1];
        [_titleMarr addObject:arr2];
        [_titleMarr addObject:arr3];
        [_titleMarr addObject:arr4];
    }
    return _titleMarr;
}

-(NSMutableArray *)typeMarr {
    if (!_typeMarr) {
        _typeMarr = [NSMutableArray array];
        NSArray *arr1 = @[@"1", @"1", @"1"];
        NSArray *arr2 = @[@"0", @"0", @"0", @"0"];
        NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"", @"", nil];
//        NSArray *arr4 = @[@"1", @"1", @"0", @"0", @"1"];
        NSArray *arr4 = @[@"0", @"0", @"0", @"0", @"1", @"1",@"1", @"1",@"1", @"1", @"1",  @"1",  @"1",  @"1",  @"1",  @"1",  @"2"];
        [_typeMarr addObject:arr1];
        [_typeMarr addObject:arr2];
        [_typeMarr addObject:arr3];
        [_typeMarr addObject:arr4];
    }
    return _typeMarr;
}

-(NSMutableArray *)rightMarr {
    if (!_rightMarr) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *timeFormatStr = [dateFormatter stringFromDate:[NSDate date]];
        _rightMarr = [NSMutableArray array];
        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"", @"", @"", nil];
        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"", @"", @"", @"", nil];
        NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"", @"", nil];
//        NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"", @"", @"", @"", timeFormatStr, nil];
        NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"", @"", @"", @"", @"", @"",@"", @"", @"", @"", @"",  @"",  @"",  @"",  @"", timeFormatStr, @"", nil];
        [_rightMarr addObject:arr1];
        [_rightMarr addObject:arr2];
        [_rightMarr addObject:arr3];
        [_rightMarr addObject:arr4];
    }
    return _rightMarr;
}

-(NSMutableArray *)colorChooseArray {
    if(!_colorChooseArray) {
        _colorChooseArray = [NSMutableArray array];
    }
    return _colorChooseArray;
}

-(NSMutableArray *)sizeChooseArray {
    if (!_sizeChooseArray) {
        _sizeChooseArray = [NSMutableArray array];
    }
    return _sizeChooseArray;
}

-(NSMutableArray *)colorChooseNameArray {
    if(!_colorChooseNameArray) {
        _colorChooseNameArray = [NSMutableArray array];
    }
    return _colorChooseNameArray;
}

-(NSMutableArray *)sizeChooseNameArray {
    if (!_sizeChooseNameArray) {
        _sizeChooseNameArray = [NSMutableArray array];
    }
    return _sizeChooseNameArray;
}

-(UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
        _confirmBtn.frame = SARectMake(0, 0, 63, 44);
        [_confirmBtn addTarget:self action:@selector(clickedConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

-(NSMutableArray *)imageChooseArray
{
    if(_imageChooseArray == nil) {
        _imageChooseArray = [NSMutableArray array];
    }
    return _imageChooseArray;
}

-(NSMutableArray *)imageServerChooseArray
{
    if(_imageServerChooseArray == nil) {
        _imageServerChooseArray = [NSMutableArray array];
    }
    return _imageServerChooseArray;
}

-(NSMutableArray<SAProductChooseColorList *> *)colorSelectArray {
    if(!_colorSelectArray) {
        _colorSelectArray = [NSMutableArray array];
    }
    return _colorSelectArray;
}


-(NSMutableArray<SAProductChooseColorList *> *)sizeSelectArray {
    if (!_sizeSelectArray) {
        _sizeSelectArray = [NSMutableArray array];
    }
    return _sizeSelectArray;
}


-(NSMutableArray<SAProductSizeORColorModel *> *)sizeORColorArray {
    if (!_sizeORColorArray) {
        _sizeORColorArray = [NSMutableArray array];
    }
    return _sizeORColorArray;
}

-(NSMutableArray<SASkuFormModel *> *)skuForm {
    if(!_skuForm) {
        _skuForm = [NSMutableArray array];
    }
    return _skuForm;
}

-(SAProductAddAPI *)productAddAPI {
    if (!_productAddAPI) {
        _productAddAPI = [SAProductAddAPI apiWithDelegate:self];
    }
    return _productAddAPI;
}

-(SAproductUploadAPI *)productUploadAPI {
    if (!_productUploadAPI) {
        _productUploadAPI = [SAproductUploadAPI apiWithDelegate:self];
    }
    return _productUploadAPI;
}


@end


